package com.liang.crawlerjd2.task;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liang.crawlerjd2.pojo.Item;
import com.liang.crawlerjd2.service.ItemService;
import com.liang.crawlerjd2.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class ItemTask {

    @Autowired
    private HttpClientUtil httpClientUtil;

    @Autowired
    private ItemService itemService;

    @Autowired
    private ObjectMapper objectMapper;

    @Scheduled(fixedDelay=100000)
    public void itemTask() throws Exception{
        log.info("=================执行任务：爬取京东手机商品数据==================");
        String url = "https://search.jd.com/search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&cid2=653&cid3=655&s=54&click=0&page=";

        // 作为Demo，我们只抓取手机分类下前五页数据（京东的分页是1 3 5，代表第1 2 3页）
        for (int i = 1; i < 10; i=i+2) {
            String html = httpClientUtil.doGetHtml(url + i);
            // 解析HTML获取商品数据并存储
            this.parse(html);
        }
    }

    private void parse(String html) throws IOException {
        // 解析HTML获取DOM对象
        Document doc = Jsoup.parse(html);
        // 获取当前页面所有spu
        Elements spuElements = doc.select("div#J_goodsList > ul > li");
        System.out.println("进没进0");
        for (Element spuElement : spuElements) {
            List<Item> itemToSave=new ArrayList<>();

            //得到Item.spuId
            //long spuId=Long.parseLong(spuElement.attr("data-spu"));

            //获取当前spu下所有sku
            Elements skuElements = spuElement.select("li.ps-item");
            System.out.println("进没进1");

            for (Element skuElement : skuElements) {

                long skuId = Long.parseLong(skuElement.select("img[data-sku]").attr("data-sku"));

                // 根据sku查询数据
                Item item = new Item();
                item.setSku(skuId);
                //List<Item> list = itemService.list();
                //if (CollectionUtils.isNotEmpty(list)) {
                //    // 当前sku已经存在，跳过本次循环，不重复保存
                //    continue;
                //}
                System.out.println("进没进3");
                // 获取sku详情页url
                String itemDetailUrl = "https://item.jd.com/" + skuId + ".html";
                log.info("itemDetailUrl:{}",itemDetailUrl);

                // 获取sku图片（实际抓取和网页源码看到的不一样，这里根据debug看到的HTML文档结构做了调整）
                String imageUrl = "https:" + skuElement.select("img[data-sku]").first().attr("data-lazy-img");
                // 获取到的sku图片太小，n9是小图，n1是大图
                imageUrl = imageUrl.replace("/n9/", "/n1/");
                // 下载图片保存到本地，返回图片名称
                String pic = httpClientUtil.doGetImage(imageUrl);
                log.info("pic:{}",pic);
            }
        }
    }
}
